package com.wy.xinzhi.common.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.dao.IDataBaseDao;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.services.AbstractBaseService;
import com.wy.framework.services.login.LoginService;
import com.wy.xinzhi.common.pojos.TCustomer;
import com.wy.xinzhi.common.pojos.TFoodRecord;

public class FoodRecordService extends AbstractBaseService  {
	
	private static Logger logger = LoggerFactory.getLogger(LoginService.class);
	static PropertiesUtil p = new PropertiesUtil();
	
	Date d = new Date(System.currentTimeMillis()); // 年月日

	Date now = new Date();
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
	String dqsj = dateFormat.format(now);

	private JdbcTemplate jdbcTemplate;
	private HibernateDaoSupport hibernateDao;
	private IDataBaseDao dataBaseDao;
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public HibernateDaoSupport getHibernateDao() {
		return hibernateDao;
	}
	public void setHibernateDao(HibernateDaoSupport hibernateDao) {
		this.hibernateDao = hibernateDao;
	}
	public IDataBaseDao getDataBaseDao() {
		return dataBaseDao;
	}
	public void setDataBaseDao(IDataBaseDao dataBaseDao) {
		this.dataBaseDao = dataBaseDao;
	}
	/**
	 * tanc
	 * 上传饮食记录
	 * @throws ServiceException 
	 */
	public void saveFoodRec(TFoodRecord foodRecord) throws ServiceException {
		try {
			hibernateDao.getHibernateTemplate().saveOrUpdate(foodRecord);
		}catch (DataAccessException e) {
			logger.error("系统错误:{}", e);
			throw new ServiceException(e);
		}
	}
	
	/**
	 * tanc
	 * 根据月份获取卡路里量以及专家点评信息
	 */
	public List<Map<String,Object>> getFoodRecByMonth(String customerId,String month) {
		String sql = "SELECT t.*,DATE_FORMAT(T.`create_date`,'%d') AS fday,DATE_FORMAT(T.`create_date`,'%Y-%m-%d') AS sday,DATE_FORMAT(T.`create_date`,'%w') AS wday FROM t_food_Record T WHERE DATE_FORMAT(T.`create_date`,'%Y%m') ='"+month+"' AND t.`customer_id`='"+customerId+"' ORDER BY t.`create_date`  ";
		List<Map<String, Object>> resultList = new ArrayList();
		resultList = dataBaseDao.getJdbcTemplate().queryForList(sql.toString());
		logger.debug("个人指标统计图表，详细："+sql.toString());
		return resultList; 
	}
	public List<Map<String,Object>> queryFoodRecord(String customerId) {
		StringBuffer sql = new StringBuffer("SELECT `re_id`,`customer_id`,`breakfast`,`lunch`,`dinner`,`add_dinner`,`feedback`,`user_id`,DATE_FORMAT(`create_date`,'%Y-%m-%d %H:%i:%s') AS create_date,DATE_FORMAT(`feedback_date`,'%Y-%m-%d %H:%i:%s') AS feedback_date,`res1`,`res2` FROM `t_food_record`  where customer_id=  "+customerId);
		List<Map<String, Object>> resultList = new ArrayList();
		resultList = dataBaseDao.getJdbcTemplate().queryForList(sql.toString());
		logger.debug("查询客户"+customerId+"的饮食信息列表，详细："+sql.toString());
		return resultList;
	}
	
	public TFoodRecord getFoodRecordById(String reId)
	{
		TFoodRecord record = null;
		List list = hibernateDao.getHibernateTemplate().find(" FROM TFoodRecord T WHERE T.reId = ?", reId);
		if(list.size()>0)
		{
			record=(TFoodRecord) list.get(0);
			return record;
		}
		return null;
	}
	
	public void deleteFoodRecordById(String reId)
	{
		TFoodRecord record = new TFoodRecord();
		record.setReId(reId);
		hibernateDao.getHibernateTemplate().delete(record);
	}
}
